package cn.lzm.serializer;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import lombok.SneakyThrows;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * @author lzm
 * @Date 2024/7/28
 * @description
 */
public class HessianSerializer implements Serializer {

    @SneakyThrows
    @Override
    public <T> byte[] serialize(T data) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(baos);
        output.writeObject(data);
        return baos.toByteArray();
    }

    @SneakyThrows
    @Override
    @SuppressWarnings("all")
    public <T> T deserialize(byte[] data, Class<T> clazz) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        HessianInput input = new HessianInput(bais);
        return (T) input.readObject(clazz);
    }


}
